<template>
	<div class="content">
		<div class="content-header">
			<el-breadcrumb separator="/">
				<el-breadcrumb-item>Demo</el-breadcrumb-item>
				<el-breadcrumb-item>
					<b>商品类别</b>
				</el-breadcrumb-item>
			</el-breadcrumb>
		</div>
		<div class="content-body">
			<el-scrollbar>
				<div class="panel">
					<el-form :model="goodsCategorySearchForm" ref="goodsCategorySearchForm" inline label-width="100px">
						<el-row>
							<el-col :md="24">
								<el-form-item label="类别名称">
									<el-input v-model="goodsCategorySearchForm.categoryName"></el-input>
								</el-form-item>
							</el-col>
						</el-row>
						<div class="search-btn">
							<div class="search-btn-left">
								<el-button type="success" icon="el-icon-plus" @click="handleAdd">新增</el-button>
							</div>
							<div class="search-btn-right">
								<el-button type="primary" icon="el-icon-search" @click="handleSearch">查询</el-button>
								<el-button icon="el-icon-refresh" @click="resetForm">重置</el-button>
							</div>
						</div>
					</el-form>
					<el-table :data="tableData" border stripe style="width: 100%">
						<el-table-column label="序号" width="60" header-align="left" align="center">
							<template slot-scope="scope">{{scope.$index + 1 + (tableCurrentPage-1)*tablePageSize}}</template>
						</el-table-column>
						<el-table-column label="类别名称" prop="categoryName"></el-table-column>
						<el-table-column label="操作" min-width="150px">
							<template slot-scope="scope">
								<el-button type="warning" @click="handleUpdate(scope.row.id)">编辑</el-button>
								<el-button type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
							</template>
						</el-table-column>
					</el-table>
					<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
						:current-page.sync="tableCurrentPage" :page-sizes="[10, 20, 30, 50]"
						:page-size.sync="tablePageSize" layout="total, jumper, prev, pager, next, sizes"
						:total="tableTotal"></el-pagination>
				</div>
			</el-scrollbar>
			<GoodsCategory ref="goodsCategory"></GoodsCategory>
		</div>
	</div>
</template>

<script>
import GoodsCategory from "./GoodsCategory";
export default {
	components: {
		GoodsCategory
	},
	data() {
		return {
			goodsCategorySearchForm: {
				categoryName: null
			},
			tableTotal: 0,
			tableCurrentPage: 1,
			tablePageSize: 10,
			tableData: []
		};
	},
	mounted() {
		this.getTableData(this.tableCurrentPage, this.tablePageSize);
	},
	methods: {
		handleSizeChange(val) {
			this.getTableData(this.tableCurrentPage, val);
		},
		handleCurrentChange(val) {
			this.getTableData(val, this.tablePageSize);
		},
		handleSearch() {
			this.getTableData(this.tableCurrentPage, this.tablePageSize);
		},
		getTableData(page, pageSize) {
			
			let pageParams = '?page=' + page + '&rows=' + pageSize;

			this.$api.post('/goodsCategory/findByPage' + pageParams, this.goodsCategorySearchForm).then(
				response => {
					this.tableData = response.data.list;
					this.tableTotal = response.data.total;
				}
			);
		},
		resetForm() {
			this.goodsCategorySearchForm = {
				categoryName: null
			};
			this.getTableData(1, this.tablePageSize);
		},
		handleAdd() {
			this.$refs.goodsCategory.initForm();
		},
		handleUpdate(id) {
			this.$refs.goodsCategory.initForm(id);
		},
		handleDelete(id) {
			this.$confirm("确认删除?", "提示", {
				confirmButtonText: "确定",
				cancelButtonText: "取消",
				type: "warning"
			}).then(() => {
				this.$api.post('/goodsCategory/delete', {id:id}).then(response => {
					if (response.success) {
						this.$message.success(response.message);
						this.handleSearch();
					} else {
						this.$message.error(response.message);
					}
				});
			}).catch(() => {});
		}
	}
};
</script>